******************************************************************************
*VIX
******************************************************************************

****************************
* Data prior to 2003
****************************

local today=c(current_date)

import excel "https://cdn.cboe.com/resources/us/indices/vxoarchive.xls", clear cellrange(A3) firstrow

rename *, lower
drop if missing(date)

*Save data as an Excel with today's date in name
export excel "../data/vix/raw_data/vxo_archive_`today'.xlsx", replace firstrow(variables)

*Replace some faulty entry
replace close="32.50" if date==td(18Oct2000)

destring close, replace

keep date close 
rename close vix

tempfile vix 
save 	`vix' 

*Data from 2004 onwards
//Note: I use the VXO in order to be consistent across the sample periods
import delimited "https://cdn.cboe.com/resources/us/indices/vxocurrent.csv", clear rowrange(3) varnames(3)

*Save data as an Excel with today's date in name
export excel "../data/vix/raw_data/vxo_current_`today'.xlsx", replace firstrow(variables)

**format date
drop if missing(date)
rename date stringdate
generate date = date(stringdate, "MDY")
format date %d
order date
drop stringdate

keep date close 
rename close vix

tabstat date, stat(min max) format(%td)

append using `vix' 
sort date

*Use original VIX for data in 2021
preserve 
	import delimited "https://cdn.cboe.com/api/global/us_indices/daily_prices/VIX_History.csv", clear 

	**format date
	drop if missing(date)
	rename date stringdate
	generate date = date(stringdate, "MDY")
	format date %d
	order date
	drop stringdate
	
	**keep only relevant dates
	keep if date >= td(01jan2021)
	keep if date < td(01jul2021)
	
	keep date close
	
	tempfile vix_2021
	save	`vix_2021'
restore 

merge 1:1 date using `vix_2021'
regress vix close 
predict vix_pred 
replace vix = vix_pred if missing(vix)
drop close vix_pred _merge 

sort date

**Variable label for clarity
label variable vix "VIX Volatility Index (CBOE)"

*Save
compress
save "../data/vix/clean_data/vix.dta", replace

